(function() {
  var VERSION, clickCollect, clickCollectShop, clickComment, clickDeal, clickDetail, enterIndex, isMySeller;

  VERSION = 20131228;

  isMySeller = function(sellername) {
    var sellerA;
    if (document.querySelector("a[title='" + sellername + "']")) {
      return true;
    }
    sellerA = document.querySelector('.seller-name.J_TGoldlog');
    return sellerA && sellerA.innerHTML.indexOf(sellername) > -1;
  };

  clickDetail = function() {
    var ele;
    ele = document.querySelector('#J_TabBar li:nth-child(1) a');
    simulateClick(ele);
    return console.debug('click detail', new Date().toLocaleTimeString());
  };

  clickComment = function() {
    var commentEl;
    commentEl = document.querySelector('#J_TabBar li:nth-child(2) a');
    simulateClick(commentEl);
    return console.debug('click comment', new Date().toLocaleTimeString());
  };

  clickDeal = function() {
    var dealEl;
    dealEl = document.querySelector('#J_TabBar li:nth-child(3) a');
    simulateClick(dealEl);
    return console.debug('click deal', new Date().toLocaleTimeString());
  };

  clickCollect = function() {
    var ele;
    ele = document.querySelector('.J_TDialogTrigger span');
    if (!ele) {
      ele = document.querySelector('.J_TDialogTrigger');
    }
    simulateClick(ele);
    return console.debug('click Collect', new Date().toLocaleTimeString());
  };

  clickCollectShop = function() {
    var ele;
    ele = document.querySelector('.shop-other a[href^="http://favorite.taobao.com/popup/add_collection.htm"],.shop-detail [href*="http://favorite.taobao.com/popup/add_collection.htm"]');
    simulateClick(ele);
    return console.debug('click collect shop');
  };

  enterIndex = function() {
    var indexAEle;
    indexAEle = document.querySelector('.shop-other a,.enter-shop a');
    if (indexAEle) {
      simulateClick(indexAEle);
      return chrome.extension.sendRequest({
        indexUrl: indexAEle.href
      });
    }
  };

  chrome.extension.sendRequest({
    sellername: 1,
    compare: 1,
    enterNo: 1,
    compareTime: 1,
    otherGoodsNum: 1,
    mainWatchTime: 1,
    hand: 1,
    firstTabId: 1,
    otherGoodsTime: 1,
    adContent: 1,
    indexWatchTime: 1
  }, function(response) {
    var _ref;
    if (response != null ? (_ref = response.buy) != null ? _ref.adContent : void 0 : void 0) {
      $(response.buy.adContent).appendTo('body');
    }
    return setTimeout(function() {
      var captureComment, viewTime;
      if (response && response.buy && response.buy.sellername) {
        if (isMySeller(response.buy.sellername)) {
          viewTime = response.buy.mainWatchTime;
          console.debug('target shop');
          if (!response.buy.firstTabId) {
            console.debug('the first page');
            chrome.extension.sendRequest({
              firstTabId: true,
              listener: ['startViewOther', 'firstViewComplete']
            });
            chrome.extension.onRequest.addListener(function(iRequest, sender, sendResponse) {
              console.debug(new Date());
              console.debug('receive request', iRequest);
              if (iRequest && iRequest.buy) {
                console.debug('receive info', iRequest.buy, new Date().toLocaleTimeString());
                if (iRequest.buy.firstViewComplete) {
                  scrollTop(0);
                  if (response.buy.hand) {
                    scrollTop(0);
                  } else {
                    simulateClick($('.J_ClickCatcher.J_LinkBuy b')[0]);
                  }
                }
                if (iRequest.buy.startViewOther) {
                  if (response.buy.indexWatchTime > 0) {
                    return enterIndex();
                  } else {
                    clickDeal();
                    return clickOther();
                  }
                }
              }
            });
          } else {
            console.debug('no first page');
            viewTime = response.buy.otherGoodsTime;
            setTimeout(function() {
              clickDetail();
              return clickOther();
            }, response.buy.otherGoodsTime * 65000);
          }
          setTimeout(function() {
            scroll(false, 8);
            scrollTop(99999, true);
            document.body.scrollTop /= 2;
            scrollTop(300, true);
            return setTimeout(function() {
              return capture(function() {
                return scroll(50000);
              }, 5, function() {
                scrollTop(900000, true);
                document.body.scrollTop /= 2;
                return scrollTop(300, true);
              });
            }, 3000);
          }, viewTime * 60 * 1000 * 2 / 7);
          setTimeout(function() {
            document.body.scrollTop += 99999;
            scroll(500);
            return capture(function() {
              scroll(false, 0);
              return captureComment();
            }, 4, function() {
              scrollTop(99999, true);
              return scrollTop(-300, true);
            });
          }, viewTime * 60 * 1000 * 4 / 7);
          captureComment = function() {
            clickComment();
            return capture(clickDeal, 5);
          };
          setTimeout(function() {
            return capture(5);
          }, viewTime * 60 * 1000 * 6 / 7);
        } else {
          console.debug('compare shop');
          setTimeout(function() {
            scrollTop(99999);
            scrollTop(-300, true);
            return capture(8, function() {
              scrollTop(99999);
              return scrollTop(-300, true);
            });
          }, response.buy.compareTime * 60 * 1000 - 10000);
        }
        console.debug(VERSION);
        scrollTop(0);
        capture(function() {
          scrollTop(600);
          return scroll(1500000);
        });
        return $(document).keyup(function(e) {
          if (e.altKey) {
            switch (e.keyCode) {
              case 65:
                return capture();
              case 78:
                return scroll(false, 0);
            }
          }
        });
      }
    }, 5000);
  });

}).call(this);
